import multiprocessing
import threading

from Detection import Center_tool, Circle_tool2


class Tool:
    def __init__(self, video=None):
        self.center_tool = Center_tool.Center_tool()
        self.circle_tool = Circle_tool2.Circle_tool2()

        self.video = video
        self.i_o_img_q = multiprocessing.Queue()
        self.o_b_img_q = multiprocessing.Queue()
        self.o_out_img_q = multiprocessing.Queue()
        self.o_msg_q = multiprocessing.Queue()

        self.x = -1
        self.y = -1
        self.d = -1

        self.b_img = None
        self.out_img = None

        self.input_process = None
        self.deal_process = None

    def set_video(self, video):
        self.video = video

    def input_video(self):
        while True:
            if self.video is not None and self.video.img is not None:
                self.i_o_img_q.put(self.video.img)

                self.x, self.y, self.d = self.o_msg_q.get()
                self.b_img = self.o_b_img_q.get()
                self.out_img = self.o_out_img_q.get()

    def gb_deal(self, i_q, b_q, o_q, m_q):

        while True:
            img = i_q.get()
            self.circle_tool.set_img(img)
            self.circle_tool.img_deal()
            self.center_tool.set(self.circle_tool.x, self.circle_tool.y, self.circle_tool.r)
            self.circle_tool.distance = self.center_tool.d
            o_q.put(self.circle_tool.output_img)
            b_q.put(self.circle_tool.b_img)
            m_q.put((self.center_tool.get_X_move(), self.center_tool.get_Y_move(), self.center_tool.d))

    def start(self):
        input_process = threading.Thread(target=self.input_video)
        self.deal_process = multiprocessing.Process(target=self.gb_deal, args=(self.i_o_img_q,
                                                                               self.o_b_img_q,
                                                                               self.o_out_img_q,
                                                                               self.o_msg_q,))
        input_process.start()
        self.deal_process.start()

    def get_X_move(self):
        return self.x

    def get_Y_move(self):
        return self.y

    def get_D_move(self):
        return self.d

    def get_out_img(self):
        return self.out_img

    def get_b_img(self):
        return self.b_img


if __name__ == '__main__':
    tool = Tool()
    tool.set_video(None)  # 设置 video
    tool.start()
    tool.stop()
